﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using Bll;
using Common;
using Web.Extension;
using Microsoft.AspNetCore.Authorization;
using Web.Filter;

namespace Web.Controllers
{
    /// <summary>
    /// 此接口替换stu目录下的对应接口
    /// </summary>
    [Route("api/")]
    [ApiController]
    [QueryFilter]
    public class CourseController : MyBaseController<Course>
    {
        ICourseBll bll;
        public IChapterBll chapterBll { get; set; }
        public CourseController(ICourseBll bll)
        {
            this.bll = bll;
        }
        [HttpGet("course/list")]
        [ResponseCache(Duration = 60, VaryByQueryKeys = new string[] { PageParam.pageNo })]//https://www.pianshen.com/article/24171627661/
        public Result List([FromQuery] Dictionary<string, string> where)
        {
            where.Remove("SchoolId");
            where.Remove("TeacherId");
            where.Remove("StudentId");
            where.Remove("UserId");
            where.Add("Open", true.ToString());
            where.Add("Passed", true.ToString());
            return Result.Success("succeed").SetData(bll.Query(where));
        }

        // GET: api/Course/Get/5
        [HttpGet("course/{id}")]
        public Result Get(int id)
        {
            return Result.Success("succeed").SetData(bll.SelectOne(id));
        }
        [HttpGet("chapter/list/{courseId}")]
        public Result ChapterList(int courseId)
        {
            Dictionary<string, string> where = new Dictionary<string, string>();
            where.Add("CourseId", courseId.ToString());
            where.Add("ParentId", null);
            //return Result.Success("succeed").SetData(chapterBll.SelectAll(o => o.CourseId == courseId && o.ParentId == null));
            return Result.Success("succeed").SetData(chapterBll.SelectAll(where,"detail,course,teacher"));
        }

        // GET: api/Course/Get/5
        [HttpGet("chapter/{id}")]
        public Result ChapterGet(int id)
        {
            return Result.Success("succeed").SetData(chapterBll.SelectOne(id));
        }
    }
}
